package com.wejoy.video.middle.station.data.service.detection.impl;

import com.wejoy.video.middle.station.data.enums.dict.EnumStatus;
import com.wejoy.video.middle.station.data.mapper.detection.IDetectionIntervarMapper;
import com.wejoy.video.middle.station.data.domain.detection.DetectionIntervar;
import com.wejoy.video.middle.station.data.service.common.IIdWorkerService;
import com.wejoy.video.middle.station.data.service.detection.IDetectionIntervarService;
import com.wejoy.video.middle.station.data.util.WJAssert;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 报警检测间隔
 *
 * @author miracle_j
 * @date 2024年04月08日 17:40:43
 */
@Service
public class DetectionIntervarServiceImpl implements IDetectionIntervarService {
    @Autowired
    private IDetectionIntervarMapper detectionIntervarMapper;
    @Autowired
    private IIdWorkerService idWorkerService;

    @Override
    public void save(DetectionIntervar req, String operator) {
        req.setOperator(operator);
        if (null == req.getId()) {
            insertSelective(req);
        } else {
            updateSelectiveById(req);
        }
    }

    @Override
    public int insertSelective(DetectionIntervar req) {
        req.setId(idWorkerService.getNextId());
        req.setStatus(EnumStatus.NEW.getCode());
        checkStringLength(req, false);
        return detectionIntervarMapper.insertSelective(req);
    }

    @Override
    public int updateSelectiveById(DetectionIntervar detectionIntervar) {
        WJAssert.notNull(detectionIntervar, "入参对象不能为空");
        WJAssert.notNull(detectionIntervar.getId(), "id参数错误");
        DetectionIntervar where = new DetectionIntervar();
        where.setId(detectionIntervar.getId());
        return detectionIntervarMapper.updateSelective(detectionIntervar, where);
    }

    @Override
    public int getCount(DetectionIntervar detectionIntervar) {
        checkStringLength(detectionIntervar, true);
        return detectionIntervarMapper.getCount(detectionIntervar);
    }

    @Override
    public DetectionIntervar getOneById(Long id) {
        WJAssert.notNull(id, "id参数错误");
        DetectionIntervar where = new DetectionIntervar();
        where.setId(id);
        return detectionIntervarMapper.getOne(where);
    }

    @Override
    public DetectionIntervar getOneByType(String type) {
        WJAssert.notNull(type, "id参数错误");
        DetectionIntervar where = new DetectionIntervar();
        where.setDetectionType(type);
        return detectionIntervarMapper.getOne(where);
    }

    @Override
    public List<DetectionIntervar> getList(DetectionIntervar detectionIntervar) {
        checkStringLength(detectionIntervar, true);
        return detectionIntervarMapper.getList(detectionIntervar);
    }

    @Override
    public List<DetectionIntervar> getPageList(DetectionIntervar detectionIntervar) {
        checkStringLength(detectionIntervar, true);
        return detectionIntervarMapper.getPageList(detectionIntervar);
    }

    @Override
    public void audit(Long id, String operator) {
        DetectionIntervar req = new DetectionIntervar();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.AUDIT.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void unaudit(Long id, String operator) {
        DetectionIntervar req = new DetectionIntervar();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.NEW.getCode());
        updateSelectiveById(req);
    }

    @Override
    public void delete(Long id, String operator) {
        DetectionIntervar req = new DetectionIntervar();
        req.setId(id);
        req.setOperator(operator);
        req.setStatus(EnumStatus.DEL.getCode());
        updateSelectiveById(req);
    }

    /**
     * 参数校验注解默认使用的方法
     */
    private void checkStringLength(DetectionIntervar detectionIntervar, boolean nullAble) {
        WJAssert.notNull(detectionIntervar, "入参对象不能为空");
        WJAssert.limitMaxLength(detectionIntervar.getDetectionType(), true, 255, "检测类型不合法!");
        WJAssert.limitMaxLength(detectionIntervar.getDetectionStr(), true, 255, "检测类型描述不合法!");
        WJAssert.limitMaxLength(detectionIntervar.getOperator(), true, 100, "操作者不合法!");
        WJAssert.limitMaxLength(detectionIntervar.getRemark(), true, 255, "备注不合法!");
    }
}
